/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * <p>Project: mybatis2021 - SpringConfig
 * <p>Powered by webrx On 2021-07-19 16:31:57
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Configuration
@ComponentScan(basePackages = {"cn.webrx.mapper", "cn.webrx.entity"})
@Import({DruidConfig.class})
public class SpringConfig {
    @Bean("sf")
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource ds,PageInterceptor pi) throws IOException {
        SqlSessionFactoryBean sf = new SqlSessionFactoryBean();
        sf.setDataSource(ds);
        sf.setTypeAliasesPackage("cn.webrx.entity");
        //设置mybatis mapper xml 文件位置 resources/mappers/*.xml
        sf.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/*.xml"));

        sf.setPlugins(pi);
        return sf;
    }

    @Bean
    public PageInterceptor pi(){
        var pi = new PageInterceptor();
        return pi;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setSqlSessionFactoryBeanName("sf");
        msc.setBasePackage("cn.webrx.mapper");
        return msc;
    }
}
